﻿//
// eCRAM, a monthly activity report web application
// Copyright (C) 2010  Aptea
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Linq;
using eCRAM.Model;
using eCRAM.Tools;

namespace eCRAM.Dao
{
    /// <summary>
    /// Contains the list of all users available in the eCRAM application.
    /// </summary>
    /// 
    /// <remarks>
    /// An administrator can give eCRAM access to employes and subcontractors.
    /// A CRAM user has a username, a password, an id and can be active or inactive.
    /// A CRAM user also has a first name, familly name, its company name...
    /// 
    /// A CRAM user can define week ends, holidays, RTTs... inside its CRAM.
    /// He can also define its occupancy, values can be 0, 1/2, 1. This is the time
    /// that took the task during the day
    /// (0 = no work, 1/2 = half day work, 1 = full day work).
    /// </remarks>
    public static class ParameterRepository
    {
        /// <summary>
        /// Get all users
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Cram_Parameter> GetAll()
        {
            using (var context = new eCramDataContext())
            {
                return context.Cram_Parameters.ToList();
            }
        }


    
        /// <summary>
        /// Get user by id
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static Cram_Parameter GetById(Guid userId)
        {
            using (var context = new eCramDataContext())
            {
                return context.Cram_Parameters.Where(p => p.Id == userId).Single();
            }
        }

        public static void Update(Cram_Parameter user)
        {
            using (var context = new eCramDataContext())
            {
                LinqTools.Update(user, u=> u.Id == user.Id, context);
                context.SubmitChanges();
            }
        }


        public static Cram_Parameter GetByEnum(EnumParameter parameter)
        {
            using (var context = new eCramDataContext())
            {
                return context.Cram_Parameters.FirstOrDefault(p => p.Name == parameter.GetLabel());
            }
        }

    }
}
